<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Hash碰撞：原理、影响与解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .concept-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .concept-card:hover {
            border-color: #667eea;
            transform: translateY(-3px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.2);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            margin: 30px 0;
            text-align: center;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .info-badge {
            display: inline-flex;
            align-items: center;
            background: #f3f4f6;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 14px;
            margin: 4px;
            transition: all 0.3s ease;
        }
        
        .info-badge:hover {
            background: #e5e7eb;
            transform: scale(1.05);
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-link mr-4"></i>深入理解Hash碰撞
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                探索哈希函数的奥秘，掌握碰撞处理的艺术
            </p>
            <div class="flex justify-center space-x-4 flex-wrap">
                <span class="info-badge bg-white bg-opacity-20 text-white">
                    <i class="fas fa-code mr-2"></i>数据结构
                </span>
                <span class="info-badge bg-white bg-opacity-20 text-white">
                    <i class="fas fa-shield-alt mr-2"></i>系统安全
                </span>
                <span class="info-badge bg-white bg-opacity-20 text-white">
                    <i class="fas fa-tachometer-alt mr-2"></i>性能优化
                </span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>许多编程任务中，我们都依赖于哈希表来实现高效的数据存储和快速检索。哈希表的核心思想是通过Hash函数将数据映射到固定大小的存储位置，从而实现快速的查找。然而，由于输入数据的多样性和Hash函数输出范围的有限性，Hash碰撞——即不同的数据项经过Hash函数计算后得到相同的哈希值——成为了一个不可避免的问题。
            </p>
            <div class="highlight-box mt-6">
                <h3 class="text-2xl font-bold mb-3">
                    <i class="fas fa-exclamation-triangle mr-2"></i>为什么要关注Hash碰撞？
                </h3>
                <p class="text-lg">
                    Hash碰撞不仅会影响数据结构的性能，还可能导致数据丢失或性能严重退化。因此，了解Hash碰撞的本质、影响以及解决方法，对于我们在实际开发中设计高效、可靠的数据存储方案至关重要。
                </p>
            </div>
        </div>

        <!-- Hash Function Overview -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-function mr-3 text-purple-600"></i>Hash函数简介
            </h2>
            
            <p class="text-lg mb-6 text-gray-700">
                Hash函数（散列函数）是计算机科学中的一个重要概念，用于将任意长度的数据映射到固定长度的值。其主要作用是在存储、检索和数据验证中提供高效的查找性能。
            </p>

            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="concept-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-check-double"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Hash函数的特性</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>确定性：</strong>相同输入始终产生相同输出</li>
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>快速计算：</strong>高效的计算速度</li>
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>均匀分布：</strong>输出均匀分布到各个位置</li>
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>抗碰撞性：</strong>较低的碰撞概率</li>
                    </ul>
                </div>
                
                <div class="concept-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">Hash函数的类型</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>非加密Hash：</strong>MurmurHash、FNV等</li>
                        <li><i class="fas fa-arrow-right text-purple-600 mr-2"></i><strong>加密Hash：</strong>MD5、SHA-256等</li>
                    </ul>
                </div>
            </div>

            <!-- Mermaid Diagram -->
            <div class="mermaid">
                graph LR
                    A[输入数据] -->|Hash函数| B[固定长度哈希值]
                    B --> C[哈希表索引]
                    C --> D[存储位置]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style C fill:#bfb,stroke:#333,stroke-width:2px
                    style D fill:#fbf,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Hash Collision Concept -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-bomb mr-3 text-red-600"></i>Hash碰撞的概念
            </h2>
            
            <p class="text-lg mb-6 text-gray-700">
                Hash碰撞发生在一个Hash函数将两个或多个不同的输入值映射到相同的哈希值时。由于Hash函数的输出是一个有限长度的值，而输入数据通常是无限的，因此总会存在不同的输入映射到相同的哈希值的可能性。
            </p>

            <div class="bg-red-50 border-l-4 border-red-500 p-6 rounded-lg mb-6">
                <h3 class="text-xl font-bold mb-3 text-red-800">
                    <i class="fas fa-dove mr-2"></i>鸽巢原理
                </h3>
                <p class="text-gray-700">
                    当你将更多的对象（鸽子）分配到有限的容器（鸽巢）中时，总会有至少一个容器包含多个对象。这解释了为什么Hash碰撞是不可避免的。
                </p>
            </div>

            <!-- Collision Visualization -->
            <div class="mermaid">
                graph TD
                    A[Key1: "apple"] -->|Hash| C[Hash值: 42]
                    B[Key2: "banana"] -->|Hash| C
                    D[Key3: "cherry"] -->|Hash| E[Hash值: 17]
                    
                    style A fill:#faa,stroke:#333,stroke-width:2px
                    style B fill:#faa,stroke:#333,stroke-width:2px
                    style C fill:#f66,stroke:#333,stroke-width:3px
                    style D fill:#afa,stroke:#333,stroke-width:2px
                    style E fill:#6f6,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Impact of Hash Collisions -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-exclamation-circle mr-3 text-yellow-600"></i>Hash碰撞的影响
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="timeline-item">
                    <h3 class="text-xl font